<?php

namespace Bundle\ModuleBundle\Plugins\ButtonPlugin;

use Bundle\ModuleBundle\Plugin\Field;

class ButtonPlugin extends \Bundle\ModuleBundle\Plugin\Plugin
{
    const BUTTON_TYPE = "button";

    public function getTitle()
    {
        return $this->_configuration->title;
    }
    public function boot()
    {
        $this->setUrl($this->_configuration->url);

        $this->setType($this->_configuration->type);
    }

    public function getName()
    {

    }

    public function getUrl($raw = false, $params = array())
    {
        $field = $this->_configuration->fields['url'];
        if($raw)
        {
            return $field->getValue();
        }
        else
        {
            $splited = \explode('/', $field->getValue());
            $result = array();
            foreach($splited as $split)
            {
                if(0 == strncmp(':route', $split ,6))
                {
                    $arguments = explode(',', trim(substr($split, 6), '()'), 1);
                    $route = trim($arguments[0], '\'');

                    //@TODO BIG TASK: ADD PARAMETERS PARSING...

                    $result []= $this->_module->router->generate($route);
                }
                else
                {
                    $result []= $split;
                }
            }
            return implode('/', $result);
        }
    }

    public function __toString()
    {
        switch($this->getType())
        {
            case 'link' : return sprintf('<a href="%s">%s</a>', $this->getUrl(), $this->getTitle());
        }
    }

}